###########################################################################
#
#   Copyright 2010 American Public Media Group
#
#   This file is part of AIR2.
#
#   AIR2 is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   AIR2 is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

package AIR2::Config;
use strict;
use warnings;
use Carp;
use Data::Dump qw( dump );
use Path::Class;
use Config::IniFiles;
use Sys::Hostname;

my $hostname = hostname();
$hostname =~ s/\..+//;
my $user = $ENV{AIR2_USER} || $ENV{USER} || $ENV{REMOTE_USER} || 'nobody';
my $path_to_this_pm = file( $INC{"AIR2/Config.pm"} );
my $etc_dir
    = $path_to_this_pm->dir->absolute->parent->parent->parent . '/etc';
my $db_registry_ini = $etc_dir . "/db_registry.ini";
my $search_ini      = $etc_dir . "/search.ini";
my $dbcfg           = Config::IniFiles->new( -file => $db_registry_ini );
my $searchcfg       = Config::IniFiles->new( -file => $search_ini );

my $profile = $ENV{'AIR2_PROFILE'} || get_profile_name();

my $air2_constants = _load_constants_file(
    get_app_root()->file('app/config/air2_constants.php') );

#dump $air2_constants;

#warn "hostname=$hostname\n";
#dump $dbcfg;

# set the global app timezone based on $hostname config
our $TIMEZONE = $dbcfg->val( $profile, 'server_time_zone' );

# same for search
our $SEARCH_URI  = $searchcfg->val( $profile, 'uri' );
our $SEARCH_ROOT = $searchcfg->val( $profile, 'root' );
if ( !$SEARCH_ROOT ) {
    croak
        "Invalid search_root -- is there a profile registered in etc/search.ini for $profile?";
}

sub get_db_ini        { return $dbcfg }
sub get_search_ini    { return $searchcfg }
sub get_hostname      { return $hostname }
sub get_user          { return $user }
sub get_tz            { return $TIMEZONE }
sub get_auth_tkt_name { return $air2_constants->{'AIR2_AUTH_TKT_NAME'} }
sub get_search_root   { return Path::Class::dir($SEARCH_ROOT) }
sub get_search_uri    { return $SEARCH_URI }
sub get_search_xml    { return Path::Class::dir( $SEARCH_ROOT, 'xml' ) }
sub get_search_index  { return Path::Class::dir( $SEARCH_ROOT, 'index' ) }

sub get_search_port {
    my ($port) = ( $SEARCH_URI =~ m/:(\d+)/ );
    return $port || 80;
}

sub get_search_index_path {
    my $self = shift;
    return $self->get_search_index->subdir(@_)->stringify;
}

sub get_app_root {
    return $path_to_this_pm->dir->absolute->parent->parent->parent;
}

sub get_profile_name {
    my $root = get_app_root();
    my $profile = $root->file( 'etc', 'my_profile' );
    if ( !-s $profile ) {
        return $hostname;
    }
    my $name = $profile->slurp;
    chomp($name);
    return $name;
}

sub _load_constants_file {
    my $file = shift;
    my @buf  = $file->slurp;
    my %const;
    for my $line (@buf) {
        if ( $line =~ m/define\((['"])([A-Z\_0-9]+)\1,\ +(['"])(.+)\3\)/ ) {
            $const{$2} = $4;
        }
    }
    return \%const;
}

1;

__END__

=head2 NAME

AIR2::Config - get application configuration

=head2 SYNOPSIS

 use AIR2::Config;
 my $db_cfg = AIR2::Config::get_db_ini();
 my $tz = AIR2::Config::get_tz();  # or just $AIR2::Config::TIMEZONE;
 my $hostname = AIR2::Config::get_hostname();
 my $username = AIR2::Config::get_user();
 my $dir = AIR2::Config::get_app_root();

=head2 FUNCTIONS

=head2 get_db_ini

Returns Config::IniFiles object representing app_root/etc/db_registry.ini.

=head2 get_tz

Returns timezone configured for current hostname.

=head2 get_hostname

Returns current hostname.

=head2 get_user

Returns username of the current process. Looks at environment variables in the following
order:

=over

=item 

AIR2_USER

=item

USER

=item

REMOTE_USER

=back

Defaults to "pijuser."

=head2 get_app_root

Returns Path::Class::Dir object for the application root directory.

=head2 get_search_uri

Returns string.

=head2 get_search_root

Returns Path::Class::Dir object.

=head2 get_search_xml

Returns Path::Class::Dir object.

=head2 get_search_index

Returns Path::Class::Dir object.

=head2 get_search_port

Returns string.

=head2 get_search_index_path('subdir')

Returns full path string for 'subdir'.

=head2 get_profile_name

Returns current profile name.

=cut

